define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Myagent_list, $timeout, GoDialog, GoPage, $ionicPopup, $window, $stateParams) {
		$scope.scope = new Object();
		$scope.input = new Object();
		$scope.data = new Object();
		$scope.input.nPage = 1;
		$scope.isLoad = false;
		$scope.count = null;
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.is_init = $stateParams.is_init;
			var all = $window.innerHeight;
		//计算滚动条
		$scope.myObj = {
			"height": all - 44 + "px"
		};
			//动态设置高度
			if($scope.is_init == true) {
				$scope.init_data();
			}
		}, false);
		//页面跳转
		$scope.onshow_list = function(id) {
			GoPage.go_states("myagent_publish", { info_id: id });

		}
		$scope.init_data = function() {
			Myagent_list.agenterdemand_list(1, function(data) {
				if(data != null)
					$scope.data = data;
				else
					$scope.data = new Object();
				$scope.input.nPage = 1;
			});
		}
		$scope.scope.reason = "";
		var myPopup;
		$scope.showPopup = function(info_id, store_id, answer) {
			$scope.scope.reason = '';
			myPopup = $ionicPopup.show({ //show()
				templateUrl: 'templates/modal/resean.html',
				title: "拒绝原因",
				scope: $scope, //绑定数据，必须写这行
				buttons: [{
					text: "取消",
					type: "button-positive", //定义按钮的样式
					onTap: function() { // 当点击取消按钮时，执行该函数

					}
				}, {
					text: "提交",
					type: "button-assertive",
					onTap: function() { // 当点击保存按钮时，执行该函数
						$scope.answer(info_id, store_id, answer);
					}
				}]
			});
		};
		$scope.answer = function(info_id, store_id, answer) {
			Myagent_list.answer_agent(info_id, store_id, answer, $scope.scope.reason, function(data) {
				if(data.success == true) {
					if(answer)
						GoDialog.toast("成功通过");
					else
						GoDialog.toast("拒绝成功");
					$scope.init_data();
				} else {
					GoDialog.toast(data.msg);
					event.preventDefault();
				}

			})
		}
		var mPopup;
		$scope.show_detail = function(info_id, name) {
			Myagent_list.angter_detail(info_id, function(data) {
				$scope.detail = data;
				mPopup = $ionicPopup.show({ //show()
					templateUrl: 'templates/modal/apply_detail.html',
					title: name + "的详情",
					scope: $scope, //绑定数据，必须写这行
				});
			})
		}
		//		$scope.isLoad = false; //可以加载
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			Myagent_list.agenterdemand_list(1, function(data) {
					if(data != null) {
						$scope.data = data;
					} else
						$scope.data = new Object();
					$scope.input.nPage = 1;
				},
				function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;

					}, 1500);
				});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;

			if($scope.data.agenterdemand == undefined) {
				$scope.input.nPage = 0;

			} else
			if($scope.data.page.count <= $scope.data.agenterdemand.length) {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				if($scope.data.page.count<$scope.input.nPage*10){
					$scope.input.nPage -=1;
					$scope.count = $scope.input.nPage *10;
				}else{
					$scope.count = null;
				}
			}

			Myagent_list.agenterdemand_list($scope.input.nPage + 1, function(data) {
					if(data != null) {
						if($scope.data.agenterdemand != undefined) {
							if($scope.count!=null)
							$scope.data.agenterdemand.splice($scope.count,$scope.data.page.count-$scope.count);
							$scope.data.agenterdemand = $scope.data.agenterdemand.concat(data.agenterdemand);
							$scope.input.nPage += 1;
						} else {
							$scope.data = data;
							$scope.input.nPage = 1;
						}

					}
				},
				function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);

				});

		};
		$scope.delete_item = function(info_id) {
			Myagent_list.delete_agent(info_id, function(data) {
				if(data.success == true) {
					GoDialog.toast("删除成功");
					$scope.init_data();
				} else {
					GoDialog.toast(data.msg);
				}

			})
		}
		$scope.showStore = function(store_id) {
			GoPage.go_states('first', { 'store_id': store_id });
		}
		var htmlEl = angular.element(document.querySelector('html'));
		htmlEl.on('click', function(event) {　　
			if(event.target.nodeName === 'HTML') {　　　　
				if(myPopup) {　　　　　　
					myPopup.close();　　　　
				}
				if(mPopup) {
					mPopup.close();
				}　　　　　
			}
		});
		$scope.init_data();
	}

	controller.$inject = ['$scope', 'Myagent_list', '$timeout', 'GoDialog', 'GoPage', '$ionicPopup', '$window', '$stateParams'];

	return app.registerController("myagent_listctrl",controller);
});